﻿using Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.Forms
{
    public partial class ChangePIN : Form
    {
        private TextBox focusedTextbox = null;
        public ChangePIN()
        {
            InitializeComponent();
            foreach (TextBox tb in this.Controls.OfType<TextBox>())
            {
                tb.Enter += textBox_Enter;
            }
        }
        void textBox_Enter(object sender, EventArgs e)
        {
            focusedTextbox = (TextBox)sender;
        }
        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (focusedTextbox!=null)
                focusedTextbox.Text+= btn.Text;
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            if (focusedTextbox != null)
                focusedTextbox.Text = string.Empty;
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            if (focusedTextbox != null)
                focusedTextbox.Text = focusedTextbox.Text.Substring(0, focusedTextbox.Text.Length - 1);
        }
        private void label4_Click(object sender, EventArgs e)
        {

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            int newPIN, oldPIN;
            if (!int.TryParse(txtOldpin.Text, out oldPIN))
            {
                txtOldpin.Text = string.Empty;
                MessageBox.Show("Please Enter your valid PIN",
                                     "Invalid PIN",
                                     MessageBoxButtons.OK);
                return;
            }
            if (!int.TryParse(txtNewPin.Text, out newPIN))
            {
                txtNewPin.Text = string.Empty;
                MessageBox.Show("Please Enter valid new PIN",
                                     "Invalid PIN",
                                     MessageBoxButtons.OK);
                return;
            }
            if (!txtNewPin.Text.Equals(txtRenew.Text))
            {
                txtRenew.Text = string.Empty;
                MessageBox.Show("Re Enter Correct new PIN",
                                     "Incorrect PIN",
                                     MessageBoxButtons.OK);
                return;
            }
            if (oldPIN != Model.CurrentSession.currentAccount.PIN)
            {
                txtOldpin.Text = string.Empty;
                MessageBox.Show("Incorrect PIN",
                                     "Incorrect PIN",
                                     MessageBoxButtons.OK);
                return;
            }
            if (txtNewPin.Text.Length != 4)
            {
                txtNewPin.Text = string.Empty;
                MessageBox.Show("Please Enter 4 digit number",
                                     "Invalid PIN",
                                     MessageBoxButtons.OK);
                return;
            }
            var confirmResult = MessageBox.Show("Are you sure want to change your PIN ??",
                                     "Confirm Change PIN!!",
                                     MessageBoxButtons.YesNo);
            if (confirmResult == DialogResult.Yes)
            {
                Model.CurrentSession.currentAccount.PIN = newPIN;
                TaskManager tm = new TaskManager();
                if (tm.ChangePinCode(Model.CurrentSession.currentAccount, oldPIN))
                {
                    txtNewPin.Text = string.Empty;
                    txtOldpin.Text = string.Empty;
                    txtRenew.Text = string.Empty;
                    MessageBox.Show("PIN Successfully Changed",
                                     "Success..!",
                                     MessageBoxButtons.OK);
                }
            }

        }
    }
}
